using System;
using System.Data;
using Microsoft.CommerceServer;
using Microsoft.CommerceServer.Catalog;
using Microsoft.CommerceServer.Inventory;
using Microsoft.CommerceServer.Catalog.Tools;
namespace FindEmptyCategories
{
	public class HowTo
	{
		public const string SiteName = "CSharpSite";

		public void MarkEmptyCategories()
		{
			CatalogSiteAgent siteAgent = null;
			CatalogContext catalogContext = null;
			CategoryTools categoryTools = null;
			CatalogDefinition categoryDefinition = null;
			CatalogProperty property = null;
			CatalogItemsDataSet emptyCategories;
			ProductCatalog productCatalog;
			StaticCategory category;
			try
			{
				//Create catalog context
				siteAgent = new CatalogSiteAgent();
				siteAgent.SiteName = SiteName;
				siteAgent.IgnoreInventorySystem = true;
				siteAgent.AuthorizationMode = AuthorizationMode.NoAuthorization;
				catalogContext = CatalogContext.Create(siteAgent);
				//Create the tool context
				categoryTools = new CategoryTools(SiteName, true);

				//Get a category definition
				categoryDefinition = catalogContext.GetDefinition("AdvWorkCatDef");

				//Create a property to mark the category category emppty
				property = catalogContext.CreateProperty("IsEmpty", CatalogDataType.Boolean, 1);

				//Add the property to a category Definition
				categoryDefinition.AddProperty(property.Name, DefinitionPropertyType.NormalProperty);
				categoryDefinition.Save();

				productCatalog = catalogContext.GetCatalog("Adventure Works Catalog");

				//Get the list of empty categories
				emptyCategories = categoryTools.GetEmptyCategories(productCatalog.Name);
				//Mark each category empty
				foreach (DataRow row in emptyCategories.CatalogItems.Rows)
				{
					category = (StaticCategory)productCatalog.GetCategory(row[CatalogItemsDataSetSchema.CategoryName].ToString());
					category["IsEmpty"] = true;
					category.Save();
				}

				//ON the runtime site, browsing through the catalog shows only non-empty categories
				CategoryConfiguration categoryConfig = new CategoryConfiguration();
				categoryConfig.ChildCategories.SqlWhereClause = "IsEmpty <> 1 OR IsEmpty IS NULL";
				//Gets the root categories for the product catalog
				CategoryCollection categoryCollection = productCatalog.GetRootCategory(categoryConfig).ChildCategories;
				foreach (Category tempCategory in categoryCollection)
				{
					//Get the next level of categories
					CategoryCollection tempCollection = productCatalog.GetCategory(tempCategory.Name,
						categoryConfig).ChildCategories;
				}
			}
			catch (Exception e)
			{
				Console.WriteLine("Unexpected exception trying to mark empty categories " + e.Message);
			}
			finally 
			{
				if (categoryDefinition != null)
				{
					categoryDefinition.RemoveProperty("IsEmpty");
					categoryDefinition.Save();
					catalogContext.DeleteProperty(property.Name);
				}
			}
		}

		public static void Main(string[] args)
		{
			//Some tests have order dependency. 
			//Eg: TestVirtualCatalogsWithoutInventory must be run before TestVirtualCatalogsWithInventory
			HowTo howTo = new HowTo();
			howTo.MarkEmptyCategories();
			UnitTests unitTest = new UnitTests(SiteName, true);
			unitTest.SetupForCatalogs();
			unitTest.TestBaseCatalogsWithoutInventory();
			unitTest.TestVirtualCatalogsWithoutInventory();
			unitTest.TestVirtualCatalogsWithHierarchyChangesWithoutInventory();
			unitTest.TestVCViewWithoutInventory();
			unitTest = new UnitTests(SiteName, false);
			unitTest.SetupForInventory();
			unitTest.TestBaseCatalogsWithInventory();
			unitTest.TestVirtualCatalogsWithInventory();
			unitTest.TestVirtualCatalogsWithHierarchyChangesWithInventory();
			unitTest.TestVCViewWithInventory();
		}
	}
}
